#pragma once
#include <iostream>
#include <fstream>
#include "speaker.hpp"
#include <vector>
#include <deque>
#include <map>
#include <random>
#include <algorithm>
#include <functional>
#include <numeric>


class SpeechManager
{
public:
	void showMenu();	// Show the menus
	void exitSystem();	// Exit the system
	void initSpeech();	// Initialize the contest
	void createSpeaker(); // Create 12 contestants
	void startSpeech();	// Begin the contest
	void speechDraw();  // Drawing 
	void speechContest(); // Formally start it!  0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

	SpeechManager();
	~SpeechManager();

	std::vector<int> v1;	// First round	12 contestants
	std::vector<int> v2;	// Second round 6 contestants
	std::vector<int> vVictory;	// Winners		3 contestants
	std::map<int, Speaker> speakerMap;	// Record the information of contestants
	int index;		// Serial number
};


// To show the menus
void SpeechManager::showMenu()
{
	std::cout << "Welcome to lecturing contest system!"
		<< "\n1.Start The Contest"
		<< "\n2.Check Records Before"
		<< "\n3.Clear The Records"
		<< "\n0.Exit The System"
		<< "\nPlease choose your option:" << std::endl;
}


// To exit the system
void SpeechManager::exitSystem()
{
	std::cout << "You've exited the system." << std::endl;
	system("pause");
	system("exit");
}


// To initialize the contest
void SpeechManager::initSpeech()
{
	this->v1.clear();
	this->v2.clear();
	this->vVictory.clear();
	this->speakerMap.clear();

	// Initialize the serial number
	this->index = 1;

}


// To create 12 contestants
void SpeechManager::createSpeaker()
{
	std::string nameSeed = "ABCDEFGHIJKL";
	for (int i = 0; i < nameSeed.size(); i++)
	{
		std::string name = "Contestant";
		name += nameSeed[i];

		Speaker sp;
		sp.name = name;

		for (int j = 0; j < 2; j++)
		{
			sp.score[j] = 0;
		}

		this->v1.push_back(i + 10001);
		
		// put it into the hole 
		this->speakerMap.insert(std::make_pair(i + 10001, sp));

	}
}


// To draw
void SpeechManager::speechDraw()
{
	std::cout << "The " << this->index << "th round is drawing" << std::endl;
	std::cout << "-------------------------------------------------" << std::endl;
	std::cout << "The orders of the contestants is these:" << std::endl;
	if (this->index == 1)
	{
		std::random_shuffle(this->v1.begin(), this->v1.end());
		// ?????????????????????????????????
		for (std::vector<int>::iterator it = this->v1.begin();
			it != this->v1.end(); it++)
		{
			std::cout << *it << " ";
		}
		std::cout << "\n\n--------------------------------------" << std::endl;
		system("pause");
	}
	else if (2 == this->index)
	{

	}
}


// To start the contest
void SpeechManager::startSpeech()
{
	// The whole process of the contest	
	// Draw
	this->speechDraw();
	
	// Listen
	this->speechContest();

	// Results

	// Listen*2

	// Results

	// Results

	// Save the results to the exercise.csv

}


// Contest and Collapse
void SpeechManager::speechContest()
{
	// Start it!!!!!!!!!!!!!
	std::cout << "\t\t\tThe " << this->index << " round has started " << std::endl;

	// The group's marks
	std::multimap<double, int, std::greater<double>()>groupScore;
	int speaker_num = 0;   // Insu

	// Check which round it is
	std::vector<int>src;

	if (this->index == 1)
	{
		src = this->v1;
	}
	else if (this->index == 2)
	{
		src = this->v2;
	}

	// Enumerate
	for (std::vector<int>::iterator it = src.begin(); it != src.end(); it++)
	{
		std::deque<double>dd;
		for (int i = 0; i < 10; i++)
		{
			double mark = (std::rand() % 401 + 600) / 10.f;
			std::cout << mark << "\t";
			dd.push_back(mark);
			std::sort(dd.begin(), dd.end(), std::greater<double>());
		}

		// Choegojeon gwa choechejeon eul
		dd.pop_back();
		dd.pop_front();

		// Summary
		double s = std::accumulate(dd.begin(), dd.end(), 0.0f);
		double avg = s / dd.size();
		// Don't forget
		std::cout << "\n";
		std::cout << "Peonho: " << *it
			<< " Ileum: " << this->speakerMap[*it].name
			<< " Pyeonggunjeom: " << avg
			<< std::endl;
		std::cout << "\n";

		// Put the avg into the map
		this->speakerMap[*it].score[this->index - 1] = avg;

		// 
		groupScore.insert(std::make_pair(avg, *it));

		// 
		if (speaker_num % 6 == 0)
		{
			std::cout << "The " << speaker_num % 6 << " round " << std::endl;

			for (std::multimap<double, int,std::greater<double>()>::iterator it = groupScore.begin(); 
				it != groupScore.end(); it++)
			{
				std::cout << "Peonho: " << (*it).second << " Ileum: " << this->speakerMap[(*it).second].name
					<< " Avg: " << (*it).first << std::endl;
				std::cout << "\n";                                   
			}

			// Take out the first third persons
			int count = 0;
			for (std::multimap<double, int, std::greater<double>()>::iterator it = groupScore.begin();
				(it != groupScore.end()) && (count < 3); it++)
			{
				count++;
				if (this->index == 1)
				{
					this->v2.push_back((*it).second);
				}
				else if (this->index == 2)
				{
					this->vVictory.push_back((*it).second);
				}
			}
			// When the first group is finish,the next group should be clear
			groupScore.clear();
		}
	}
	std::cout << "______________The " << this->index << " round finished ________________\n" << std::endl;
	//
	system("pause");
}


// Constructor 
SpeechManager::SpeechManager()
{
	this->initSpeech();
	this->createSpeaker();
}


// Destructor
SpeechManager::~SpeechManager()
{

}

